$(document).ready(function () {
	$('#personal_number').attr("disabled", "disabled");
	$('#run_with_personal_number').attr("disabled", "disabled");
	var logName = $('#log_name_hidden').val();
	var debugFileName = $('#debugfile_name_hidden').val();
	var folderName = $('#folder_name_hidden').val();
	openCmdOutputTab(logName, debugFileName);
	$("#actualized_output").load('./logs/'+folderName+"/"+debugFileName);
	refresh();
	openLogFile();
	
});

function openCmdOutputTab(logName, debugFileName) {
	// var logName = $('#log_name_hidden').val();
	// var debugFileName = $('#debugfile_name_hidden').val();
	
	var widthValue = 800;
	var heightValue = 500;
	var top = (screen.height/2 - heightValue/2);
	var left = (screen.width/2 - widthValue/2);
	
	$('#run_prepared_command').click(function () {
		if ($('#command').val() == '') {			
			alert('Nie wprowadzono zmiennych.');
			return false;
		} else {
			var preparedCommand = $('#command').val();
			window.open("cmdTaskKillWindow?ln="+logName, "_blank"); // will open new tab on document ready
			window.open("cmdOutput?cmd="+preparedCommand+"&ln="+logName+"&dfn="+debugFileName, "_blank"); // will open new tab on document ready
		}
	});
	$('#run_any_prepared_command').click(function () {
		if ($('#command2').val() == '') {			
			alert('Nie wprowadzono polecenia.');
			return false;
		} else {
			var preparedCommand = $('#command2').val();
			window.open("cmdTaskKillWindow?ln="+logName, "_blank"); // will open new tab on document ready
			window.open("cmdOutput?cmd="+preparedCommand+"&ln="+logName+"&dfn="+debugFileName, "_blank"); // will open new tab on document ready
			// window.open('cmdTaskKillWindow?ln='+logName,'formpopup', 'width=' + widthValue +', height=' + heightValue + ', top=' + top + ', left=' + left + ', scrollbars');
		}
	});
	$('#run_with_personal_number').click(function () {
		if ($('#personal_number').val() == '') {			
			alert('Nie wprowadzono numeru PESEL.');
			return false;
		} 
		else if ($('#environment').val() == 0) {						
			alert('Nie wybrano środowiska testowego.');
			return false;
		} else {
			var personalNumber = $('#personal_number').val();
			var environment = $('#environment').val();
			// window.open("cmdTaskKillWindow?ln="+logName, "_blank"); // will open new tab on document ready
			// window.open("cmdOutput?persNum="+personalNumber+"&env="+environment+"&ln="+logName+"&dfn="+debugFileName, "_blank"); // will open new tab on document ready
		}
	});
	$('#run_with_order_id').click(function () {
		if ($('#order_id').val() == '') {			
			alert('Nie wprowadzono idZamówienia.');
			return false;
		} 
		else if ($('#environment').val() == 0) {						
			alert('Nie wybrano środowiska testowego.');
			return false;
		} else {
			var orderId = $('#order_id').val();
			var environment = $('#environment').val();
			window.open("cmdTaskKillWindow?ln="+logName, "_blank"); // will open new tab on document ready
			window.open("cmdOutput?ordId="+orderId+"&env="+environment+"&ln="+logName+"&dfn="+debugFileName, "_blank"); // will open new tab on document ready	
		}
	});
}

function refresh() {
	setTimeout( function () {		
		// $('#auto').fadeOut('fast').load('./logs/debugfile.txt').fadeIn('fast');
		var debugFileName = $('#dfn_hidden').val();
		var folderName = $('#fn_hidden').val();
		
		$('#actualized_output').load('./logs/'+folderName+"/"+debugFileName);
     	
		$('#actualized_output').scrollTop($('#actualized_output')[0].scrollHeight); // move to the bottom of a div
			
		var debugFileData = $('#actualized_output').text();
		
		// alert(debugFileData);
		
		if(debugFileData.contains("+ END SUITE")){
			// alert(debugFileName);
		    // alert("String Found");
		    var logName = $('#ln_hidden').val();
		    // alert(logName);
		    // alert(checkIfURLExists("./logs/"+logName));
			if (checkIfURLExists("./logs/"+folderName+"/"+logName)) {
				setTimeout( function () {
					window.location.replace("./logs/"+folderName+"/"+logName);
				}, 2000);  
		    	// window.location.replace("./logs/index.html");
			} else {
				$('#open_log_file').attr("hidden", false);
		    	// alert(checkIfURLExists("./logs/"+logName));
		   }
		}		
		refresh();
	}, 1000);
}

function openLogFile() {
	$('#open_log_file').click(function () {
		// return false;
		var logName = $('#ln_hidden').val();
		// aler(folderName);
		var folderName = $('#folder_name_hidden').val();
		// aler(folderName);
		// window.open('./logs/'+logName, "_blank"); // will open new tab on document ready		
		if (checkIfURLExists("./logs/"+folderName+"/"+logName)) {
			window.location.replace("./logs/"+folderName+"/"+logName);		    
	    	// window.location.replace("./logs/index.html");
		} else {
			alert("Nie istnieje plik o nazwie: " + logName);
   		}
	});
}

function checkIfURLExists(url)	{
    var http = new XMLHttpRequest();
    http.open('HEAD', url, false);
    http.send();
    return http.status != 404;
}

